package com.dyh.quickdemo.depRm;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

//@Configuration
public class EnhanceBeanRegister implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
                .getAutowireCapableBeanFactory();

        String beanName = "callSystemAFacade";
        CallSystemAFacadeIntf oldBean = applicationContext.getBean(CallSystemAFacade.class);
        InvocationHandler handler = new CommonInvocationHandler(oldBean);
        CallSystemAFacadeIntf newBean = (CallSystemAFacadeIntf) Proxy.newProxyInstance(
                oldBean.getClass().getClassLoader(), oldBean.getClass().getInterfaces(), handler);
        beanFactory.removeBeanDefinition(beanName);
        beanFactory.registerSingleton(beanName,newBean);


//        for (LambdaWrap lambdaWrap : MapRegistMedium.all()) {
//            beanFactory.registerSingleton(lambdaWrap.getId(),lambdaWrap.getLambda());
//        }
    }


}
